package com.tyl.springbootjdbc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class JDBCController{

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @ResponseBody
    @RequestMapping("/getList")
    public List<Map<String, Object>> getStudentAll(){
        String sql="select *from mybatis.student";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
    @ResponseBody
    @RequestMapping("/adduser")
    public String addUser(){
        String sql="insert into mybatis.user (id,name,pwd)values(4,'111','111')";
        jdbcTemplate.update(sql);
        return "addUser-OK";
    }

    @ResponseBody
    @RequestMapping("/updateUser")
    public String updateUser(){
        String sql="update mybatis.user set name='2222',pwd ='222'  where id=4 ;";
        jdbcTemplate.update(sql);
        return "updateUser-OK";
    }

    @ResponseBody
    @RequestMapping("/deleteUser")
    public String deleteUser(){
        String sql="delete from mybatis.user where id=4";
        jdbcTemplate.update(sql);
        return "deleteUser-OK";
    }
}
